package com.woniuxy.lrz.carlproject.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.common.Log;
import com.woniuxy.lrz.carlproject.common.Result;
import com.woniuxy.lrz.carlproject.controller.form.serviceform.updateServiceStatusForm;
import com.woniuxy.lrz.carlproject.model.ServiceInfo;
import com.woniuxy.lrz.carlproject.model.ShopService;
import com.woniuxy.lrz.carlproject.service.ShopServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 门店服务表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Slf4j
@Api("门店服务接口")
@RequestMapping("/shopService")
public class ShopServiceController {

    @Resource
    private ShopServiceService shopServiceService;


    @GetMapping("/getAllService")
    @Log("查看门店服务")
    @ApiOperation("查看门店服务")
    @ApiImplicitParam("门店id")
    public Result getAllService(int current,@RequestParam(defaultValue = "10")  int size){
        int shopId = Integer.parseInt((String) StpUtil.getLoginId());
        log.info("门店id:{}",shopId);
        if (shopId<1){
            throw  new AppException("参数错误");
        }
        Page<ShopService> servicePage = shopServiceService.selectService(current, size, shopId);
        return Result.success(servicePage);
    }

    @GetMapping("/addService")
    @Log("添加门店服务")
    @ApiOperation("添加门店服务")
    @ApiImplicitParam("服务id")
    public Result addService( int serviceId){
        int shopId = Integer.parseInt((String) StpUtil.getLoginId());
        log.info("服务id:{}",serviceId);
        if ( serviceId<1 ) {
            throw  new AppException("参数错误");
         }
        if (shopId<1) {
            throw  new AppException("参数错误");
        }
        shopServiceService.addService(serviceId,shopId);
        return Result.success();
    }

    @DeleteMapping("/deleteService")
    @Log("删除门店服务")
    @ApiOperation("删除门店服务")
    @ApiImplicitParam("门店id，服务id")
    public Result deleteService(Integer serviceId){
        int shopId = Integer.parseInt((String) StpUtil.getLoginId());
        log.info("服务id:{}",serviceId);
        if (serviceId == null || serviceId.equals("")||serviceId<1) {
            throw  new AppException("参数错误");
        }
        if (shopId<1) {
            throw  new AppException("参数错误");
        }
        shopServiceService.deleteService(serviceId,shopId);
        return Result.success();
    }

    @PutMapping("/updateServiceStatus")
    @Log("修改门店服务状态")
    @ApiOperation("修改门店服务状态")
    @ApiImplicitParam("门店id,服务id,服务状态值")
    public  Result updateServiceStatus(@Valid @RequestBody updateServiceStatusForm updateServiceStatusForm){
        int shopId = Integer.parseInt((String) StpUtil.getLoginId());
        log.info("服务id:{}门店id{}状态{}",updateServiceStatusForm.getServiceId(),shopId,updateServiceStatusForm.getStatus());

        shopServiceService.updateStatus(updateServiceStatusForm.getServiceId(),shopId,updateServiceStatusForm.getStatus());
        return Result.success();
    }
}
